<template>
  <div class="my-works-page">
    <nav-bar />
    
    <div class="page-content">
      <h1 class="page-title">我的作品</h1>
      
      <div class="image-grid">
        <!-- 生成的图片列表 -->
        <div 
          class="image-card" 
          v-for="(item, index) in imageList" 
          :key="index" 
          @click="previewImage(item.url, item.revised_prompt)"
        >
          <div class="image-frame">
            <img :src="item.url" alt="生成的图像" class="generated-image" />
          </div>
          <div class="image-info">
            <p class="image-prompt">{{ item.revised_prompt }}</p>
            <div class="image-actions">
              <button class="action-btn primary" @click.stop="downloadImage(item.url)">下载</button>
              <button class="action-btn" @click.stop="shareImage(item)">分享</button>
              <button class="action-btn danger" @click.stop="deleteImage(index)">删除</button>
            </div>
          </div>
        </div>
        
        <!-- 无图像数据时显示 -->
        <div class="no-image" v-if="imageList.length === 0">
          <icon-image class="no-image-icon" />
          <p class="no-image-text">暂无图像数据，请先生成图像</p>
          <button class="action-btn primary" @click="goToGeneration">创建图像</button>
        </div>
      </div>
      
      <!-- 分页提示 -->
      <div class="pagination-hint" v-if="imageList.length > 0">
        已加载全部，共 {{ imageList.length }} 张图片
      </div>
    </div>
    
    <!-- 图片预览模态框 -->
    <div class="image-preview-modal" v-if="previewVisible" @click="closePreview">
      <div class="modal-content" @click.stop>
        <div class="modal-close" @click="closePreview">×</div>
        <div class="preview-image-container">
          <img :src="previewImageUrl" alt="预览图片" class="preview-image" />
        </div>
        <div class="preview-info">
          <p class="preview-prompt">{{ previewPrompt }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { Message } from '@arco-design/web-vue'
import { IconImage } from '@arco-design/web-vue/es/icon'
import { useRouter } from 'vue-router'
import NavBar from '../components/NavBar.vue'

const router = useRouter()

// 图像数据接口
interface ImageData {
  url: string
  revised_prompt: string
  created_at?: number
}

// 图像列表
const imageList = ref<ImageData[]>([])

// 图像预览
const previewVisible = ref(false)
const previewImageUrl = ref('')
const previewPrompt = ref('')

// 预览图片
const previewImage = (url: string, prompt: string) => {
  previewImageUrl.value = url
  previewPrompt.value = prompt
  previewVisible.value = true
  // 禁止背景滚动
  document.body.style.overflow = 'hidden'
}

// 关闭预览
const closePreview = () => {
  previewVisible.value = false
  // 恢复背景滚动
  document.body.style.overflow = ''
}

// 从本地存储获取图像数据
onMounted(() => {
  loadImageList()
})

// 加载图像列表
const loadImageList = () => {
  // 尝试从本地存储获取
  const storedData = localStorage.getItem('gilipu_generated_image')
  if (storedData) {
    try {
      const parsedData = JSON.parse(storedData)
      
      // 处理旧版本的单个图像数据格式
      if (parsedData && !Array.isArray(parsedData) && parsedData.url) {
        // 将单个对象转换为数组格式
        imageList.value = [{
          url: parsedData.url,
          revised_prompt: parsedData.revised_prompt,
          created_at: Date.now()
        }]
        
        // 保存为新格式
        saveImageList()
      } else if (Array.isArray(parsedData)) {
        // 新格式，直接使用
        imageList.value = parsedData
      }
    } catch (e) {
      console.error('解析存储的图像数据失败:', e)
      // 初始化为空数组
      imageList.value = []
      saveImageList()
    }
  }
}

// 保存图像列表到本地存储
const saveImageList = () => {
  try {
    localStorage.setItem('gilipu_generated_image', JSON.stringify(imageList.value))
  } catch (e) {
    console.error('保存图像列表失败:', e)
  }
}

// 删除图片
const deleteImage = (index: number) => {
  if (index >= 0 && index < imageList.value.length) {
    imageList.value.splice(index, 1)
    saveImageList()
    Message.success('已删除图片')
  }
}

// 下载图片
const downloadImage = (url: string) => {
  const link = document.createElement('a')
  link.href = url
  link.download = `gilipu-image-${Date.now()}.png`
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
  
  Message.success('图片下载中...')
}

// 分享图片
const shareImage = (image: ImageData) => {
  if (navigator.share) {
    navigator.share({
      title: '吉卜力风格图像',
      text: image.revised_prompt,
      url: window.location.href
    }).then(() => {
      Message.success('分享成功')
    }).catch((error) => {
      console.error('分享失败:', error)
      Message.error('分享失败，请手动复制链接分享')
    })
  } else {
    // 复制链接到剪贴板
    const url = window.location.href
    navigator.clipboard.writeText(url).then(() => {
      Message.success('链接已复制到剪贴板')
    }).catch(() => {
      Message.error('复制失败，请手动复制链接')
    })
  }
}

// 跳转到图像生成页面
const goToGeneration = () => {
  router.push('/image-generation')
}
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body, html {
  overflow-x: hidden;
  width: 100%;
  height: 100%;
}

.my-works-page {
  min-height: 100vh;
  width: 100vw;
  background-color: #000000;
  color: #ffffff;
  padding-top: 64px; /* 为导航栏留出空间 */
  display: flex;
  flex-direction: column;
  overflow-x: hidden; /* 防止水平滚动条 */
  position: absolute;
  left: 0;
  top: 0;
}

.page-content {
  flex: 1;
  width: 100%;
  padding: 30px 0;
  display: flex;
  flex-direction: column;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  margin: 0 0 30px;
  color: #ffffff;
  text-align: center;
}

.image-grid {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 20px;
  padding: 0 20px;
  margin-bottom: 20px;
  width: 100%;
}

.image-card {
  flex: 0 0 auto;
  width: 300px;
  border-radius: 12px;
  overflow: hidden;
  background-color: #111;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  cursor: pointer;
  margin-bottom: 20px;
}

.image-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3);
}

.image-frame {
  position: relative;
  width: 100%;
  height: 200px;
  overflow: hidden;
}

.image-frame::after {
  content: '点击预览';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 16px;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.image-card:hover .image-frame::after {
  opacity: 1;
}

.generated-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.image-card:hover .generated-image {
  transform: scale(1.05);
}

.image-info {
  padding: 15px;
}

.image-prompt {
  margin: 0 0 15px;
  font-size: 14px;
  line-height: 1.4;
  color: #ccc;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.image-actions {
  display: flex;
  gap: 8px;
}

.action-btn {
  padding: 8px 12px;
  border: none;
  border-radius: 4px;
  background-color: #333;
  color: white;
  font-size: 14px;
  cursor: pointer;
  transition: background-color 0.2s;
}

.action-btn:hover {
  background-color: #444;
}

.action-btn.primary {
  background-color: #1890ff;
}

.action-btn.primary:hover {
  background-color: #40a9ff;
}

.action-btn.danger {
  background-color: #ff4d4f;
}

.action-btn.danger:hover {
  background-color: #ff7875;
}

.no-image {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 50px 0;
}

.no-image-icon {
  font-size: 48px;
  color: #333;
  margin-bottom: 20px;
}

.no-image-text {
  color: #666;
  margin-bottom: 20px;
  font-size: 16px;
}

.pagination-hint {
  text-align: center;
  color: #666;
  font-size: 14px;
  margin-top: 20px;
}

/* 图片预览模态框 */
.image-preview-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.9);
  z-index: 1000;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  box-sizing: border-box;
}

.modal-content {
  position: relative;
  max-width: 90%;
  max-height: 90%;
  display: flex;
  flex-direction: column;
  background-color: #111;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}

.modal-close {
  position: absolute;
  top: 15px;
  right: 15px;
  width: 30px;
  height: 30px;
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;
  cursor: pointer;
  z-index: 10;
  transition: background-color 0.2s;
}

.modal-close:hover {
  background-color: rgba(0, 0, 0, 0.8);
}

.preview-image-container {
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.preview-image {
  max-width: 100%;
  max-height: 70vh;
  object-fit: contain;
}

.preview-info {
  padding: 15px 20px;
  background-color: #111;
}

.preview-prompt {
  margin: 0;
  font-size: 14px;
  color: #fff;
  line-height: 1.5;
}
</style>
